package com.longcai.cm.api.api4home;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.cm.bean.Page;
import com.longcai.cm.bean.R;
import com.longcai.cm.common.utils.StringUtils;
import com.longcai.cm.domain.*;
import com.longcai.cm.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author libingxing
 */
@RestController
@Api(tags = "首页相关接口")
@RequestMapping("/api/home")
public class ApiHomeController {

    @Resource
    private JaNavigatorService navigatorService;

    @Resource
    private JaServiceInfoService serviceInfoService;

    @Resource
    private JaShopService shopService;

    @Resource
    private JaBannerService bannerService;

    @Resource
    private ZThirdCarService thirdCarService;

    @Resource
    private VGoodInfoListService goodInfoListService;

    @PostMapping("/navigatorList")
    @ApiOperation("金刚区")
    public R<List<JaNavigator>> navigatorList(
    ) {
        return R.success(navigatorService.lambdaQuery().eq(JaNavigator::getIsShow, 1).orderByAsc(JaNavigator::getOrderNo).list());
    }

    @PostMapping("/bannerList")
    @ApiOperation("轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "1轮播图 2广告图", name = "type", dataType = "int", required = true)
    })
    public R<List<JaBanner>> bannerList(
            String type
    ) {
        final LambdaQueryWrapper<JaBanner> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(type)) {
            query.eq(JaBanner::getType, type);
        }
        query.eq(JaBanner::getIsShow, 1);
        query.orderByDesc(JaBanner::getCreateTime);
        return R.success(bannerService.list(query));
    }

    @PostMapping("/serviceInfoList")
    @ApiOperation("服务类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "首页不传 1不带加油 2只有加油", name = "flag", dataType = "int", required = true)
    })
    public R<List<JaServiceInfo>> serviceInfoList(
            String flag
    ) {
        final LambdaQueryWrapper<JaServiceInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JaServiceInfo::getPid,"0");
        wrapper.eq(JaServiceInfo::getIsShow,"1");
        wrapper.orderByAsc(JaServiceInfo::getOrderNo);
        final List<JaServiceInfo> list;
        if (StringUtils.isNotBlank(flag)){
            wrapper.ne(JaServiceInfo::getType,2);
            list = serviceInfoService.list(wrapper);
        }else{
            list = serviceInfoService.list(wrapper);
            list.add(new JaServiceInfo(("车型大全")));
            list.add(new JaServiceInfo(("优选驾校")));
            Collections.reverse(list);
        }
        return R.success(list);
    }

    @PostMapping("/shopList")
    @ApiOperation("门店列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", name = "page", dataType = "int", required = true),
            @ApiImplicitParam(value = "条数", name = "rows", dataType = "int", required = true),
            @ApiImplicitParam(value = "服务类型", name = "serviceInfo", dataType = "int", required = true),
            @ApiImplicitParam(value = "排序方式 1距离 2评价 3销量", name = "orderType", dataType = "int", required = true),
            @ApiImplicitParam(value = "是否置顶 1置顶", name = "ifTop", dataType = "int", required = true),
            @ApiImplicitParam(value = "经度", name = "lng", dataType = "int", required = true),
            @ApiImplicitParam(value = "纬度", name = "lat", dataType = "int", required = true),
            @ApiImplicitParam(value = "城市", name = "city", dataType = "int", required = true),
            @ApiImplicitParam(value = "1 加油/充电", name = "shopType", dataType = "int", required = true),
            @ApiImplicitParam(value = "搜索 门店名称", name = "keyword", dataType = "int", required = true),
    })
    public R<Page<List<JaShop>>> shopList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows,
            String serviceInfo, String lng, String lat, String city,
            @RequestParam(defaultValue = "1") Integer orderType,
            @RequestParam(defaultValue = "0") Integer ifTop,
            @RequestParam(defaultValue = "0") Integer shopType,
            String keyword
    ) {
        QueryWrapper<JaShop> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(serviceInfo)) {
            queryWrapper.like("service_info", serviceInfo);
        } else {
            if (shopType == 1) {
                queryWrapper.like("service_info", "加油")
                        .or().like("service_info", "充电");
            }
        }
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like("name", keyword);
        }
        if (StringUtils.isNotBlank(city)) {
            queryWrapper.like("city", city);
        }
        final long total = shopService.count(queryWrapper);
        queryWrapper.select("*, st_distance_sphere ( Point(" + lng + ", " + lat + "), location ) AS distance");
        if (ifTop == 1) {
            queryWrapper.orderByDesc("is_top");
        }
        switch (orderType) {
            case 2:
                queryWrapper.orderByDesc("score");
                break;
            case 3:
                queryWrapper.orderByDesc("count");
                break;
            default:
                queryWrapper.orderByAsc("distance");
                break;
        }
        final LambdaQueryWrapper<JaShop> wrapper = queryWrapper.lambda();
        final Page<List<JaShop>> result = new Page<>(page, rows, total);
        wrapper.last(result.convertPage());
        final List<JaShop> list = shopService.list(wrapper);
        for (JaShop shop : list) {
            final List<VGoodInfoList> goodInfoLists = goodInfoListService
                    .lambdaQuery().eq(VGoodInfoList::getShopId, shop.getId())
                    .eq(VGoodInfoList::getType, 1).eq(VGoodInfoList::getStatus, 1)
                    .orderByDesc(VGoodInfoList::getCreateTime)
                    .last(" limit 3 ")
                    .list();
            if (goodInfoLists.size() > 0) {
                shop.setGoodInfoLists(goodInfoLists);
            } else {
                shop.setGoodInfoLists(new ArrayList<>());
            }
        }
        result.setContent(list);
        return R.success(result);
    }

    @PostMapping("/thirdCarList")
    @ApiOperation("三方列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", name = "page", dataType = "int", required = true),
            @ApiImplicitParam(value = "条数", name = "rows", dataType = "int", required = true),
            @ApiImplicitParam(value = "分类 1打车 2租车 3代驾 ", name = "type", dataType = "int", required = true),
    })
    public R<Page<List<ZThirdCar>>> thirdCarList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows,
            @RequestParam(defaultValue = "1") Integer type
    ) {
        final LambdaQueryWrapper<ZThirdCar> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZThirdCar::getType, type);
        final long total = thirdCarService.count(wrapper);
        final Page<List<ZThirdCar>> result = new Page<>(page, rows, total);
        wrapper.last(result.convertPage());
        wrapper.orderByDesc(ZThirdCar::getCreateTime);
        final List<ZThirdCar> list = thirdCarService.list(wrapper);
        result.setContent(list);
        return R.success(result);
    }


}
